﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.ModuleButton;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.System
{
    /// <summary>
    /// 用户管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/System/[controller]")]
    public class ModuleButtonController : BaseApiController
    {
        private ModuleButtonBll moduleButtonBll = new ModuleButtonBll();

        #region GET
        /// <summary>
        /// 获取按钮权限列表
        /// </summary>
        /// <param name="moduleId">功能id</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet("{moduleId}/List")]
        public async Task<ResponseResult> GetList(string moduleId, KeywordInput input)
        {
            var data = await moduleButtonBll.GetList(moduleId);
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword));
            }
            var treeList = data.MapToList<ModuleButtonListOutput>();
            return Success(new { list = treeList.ToTree("-1") });
        }

        /// <summary>
        /// 获取按钮权限下拉列表
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        [HttpGet("{moduleId}/Selector")]
        public async Task<ResponseResult> GetSelector(string moduleId)
        {
            var data = await moduleButtonBll.GetList(moduleId);
            var treeList = data.MapToList<ModuleButtonSelectorOutput>();
            return Success(new { list = treeList.ToTree("-1") });
        }

        /// <summary>
        /// 获取按钮信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await moduleButtonBll.GetInfo(id);
            var output = data.MapTo<ModuleButtonInfoOutput>();
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Creater([FromBody] ModuleButtonCrInput input)
        {
            var entity = input.MapTo<ModuleButtonEntity>();
            var isOk = await moduleButtonBll.Create(entity);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ModuleButtonUpInput input)
        {
            var entity = input.MapTo<ModuleButtonEntity>();
            var isOk = await moduleButtonBll.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await moduleButtonBll.GetInfo(id);
            await moduleButtonBll.Delete(entity);
            return Success("删除成功");
        }

        /// <summary>
        /// 更新按钮状态
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await moduleButtonBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 0 ? 1 : 0;
            var isOk = await moduleButtonBll.Update(id, entity);
            return isOk > 0 ? Success("更新状态成功") : Error("更新状态失败");
        }
        #endregion
    }
}
